﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ArbolandiaBL;
using System.IO;

public partial class _TipsJardin : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string category = Request.QueryString["cat"] != null ? Request.QueryString["cat"] : "";
            string pn = Request.QueryString["pn"] != null ? Request.QueryString["pn"] : "";
            InitPage();
            LoadTips(category, pn);
            //Load contentText
            string fileName = "tipsjardin.txt";
            string pathfile = Server.MapPath(@"~" + @"\" + "templates" + @"\" + fileName);
            if (File.Exists(pathfile)) { var content = File.ReadAllText(pathfile); contenttext.InnerHtml = content; }

        }

    }
    protected void LoadTips(string category, string pn)
    {
        var total = new Service().GetTotalTipsJardin();
        var tipsListAll = new List<Arb_TipsJardin>();
        var catList = new Service().GetAllCategorysTipsJardin();
        if (string.IsNullOrEmpty(category))
        {
            tipsListAll = new Service().GetAllTipsJardin();
        }
        else
        {
            tipsListAll = new Service().GetAllTipsJardin(category);
        }

        //Agregamos el de default TODAS
        var divDefault = new HtmlGenericControl("div");
        var hlDefault = new HyperLink();
        hlDefault.ID = "Todos";
        hlDefault.Text = "Todos" + " (" + total.ToString() + ")";
        hlDefault.NavigateUrl = "~/TipsJardin.aspx?c=6";
        if (string.IsNullOrEmpty(category)) { hlDefault.Style.Add("color", "#a8c32d"); }
        divDefault.Controls.Add(hlDefault);
        tipsCategory.Controls.Add(divDefault);
        int _pn; Int32.TryParse(pn, out _pn);
        //llenamos paginator
        FillPaginator(tipsListAll, _pn, category);
        var tipsList = tipsListAll.Skip((_pn - 1) * 10).Take(10).ToList();

        //llenamos el lado izquierdo
        foreach (var t in catList)
        {
            //left banner
            var div = new HtmlGenericControl("div");
            var hl = new HyperLink();
            hl.ID = t.Title.ToString();
            hl.Text = t.Title + " (" + t.Total.ToString() + ")";
            hl.NavigateUrl = "~/TipsJardin.aspx?c=6&cat=" + t.Title;
            if (category == t.Title) { hl.Style.Add("color", "#a8c32d"); }
            div.Controls.Add(hl);
            tipsCategory.Controls.Add(div);
        }
        //Llenamos todos.
        lblRoute.Text = "INICIO / " + "Tips para el Jardin";
        if (!string.IsNullOrEmpty(category)) { lblRoute.Text += " / " + tipsList.FirstOrDefault().Category; }

        for (int j = 0; j < tipsList.Count(); j++)
        {
            //Tip Jardin main div
            var div = new HtmlGenericControl("div");
            div.Attributes.Add("class", "tipjardin");
            //Header Tip Jardin
            var p = new HtmlGenericControl("p");
            p.Attributes.Add("class", "menu_head");
            var a = new HtmlGenericControl("a");
            a.InnerHtml = tipsList[j].Title;
            a.Attributes.Add("class", "headertext");
            p.Controls.Add(a);
            div.Controls.Add(p);
            //Content
            var divcontent = new HtmlGenericControl("div");
            divcontent.Attributes.Add("class", "menu_body");
            //var socialPlugIn = GetSocialPlugInDiv(tipsList[j].Title, tipsList[j].Content, HttpContext.Current.Request.Url.AbsoluteUri);
            //divcontent.Controls.Add(socialPlugIn);
            var pcontent = new HtmlGenericControl("div");
            pcontent.InnerHtml = tipsList[j].Content;
            divcontent.Controls.Add(pcontent);
            div.Controls.Add(divcontent);

            //add main div 
            contenttipsjardin.Controls.Add(div);
        }
    }
    protected void FillPaginator(List<Arb_TipsJardin> list, int pn, string c)
    {
        //Paginator
        Int32 total = list.Count();
        Int32 totalPages = total / 10;
        Int32 residuo = total % 10;
        if (totalPages == 0)
        {
            totalPages = 1;
        }
        else if (residuo > 0)
        {
            totalPages += 1;
        }
        if (pn == 0) { pn = 1; }
        //Add anterior
        if (pn > 1)
        {
            var pageanterior = new Label();
            pageanterior.Text = "« Anterior";
            pageanterior.Attributes.Add("style", "padding-left:10px; padding-right:10px;");
            pageanterior.Attributes.Add("onclick", "document.location.href='TipsJardin.aspx?c=6&pn=" + (pn - 1).ToString() + (!string.IsNullOrEmpty(c) ? "&cat=" + c : "") + "';");
            paginator.Controls.Add(pageanterior);
        }

        //Add numbers
        for (int i = 1; i <= totalPages; i++)
        {
            var page = new HtmlGenericControl("span");
            page.InnerText = i.ToString();
            if (pn.ToString() == i.ToString())
            {
                page.Attributes.Add("style", "background-color: #edeff4; color: gray; border: solid 1px gray;");
            }
            page.Attributes.Add("onclick", "document.location.href='TipsJardin.aspx?c=6&pn=" + i.ToString() + (!string.IsNullOrEmpty(c) ? "&cat=" + c : "") + "';");
            paginator.Controls.Add(page);
        }

        //Add siguiente
        var pagesiguiente = new HtmlGenericControl("span");
        pagesiguiente.InnerText = "Siguiente »";
        pagesiguiente.Attributes.Add("style", "padding-left:10px; padding-right:10px;");
        var tempnext = pn + 1;
        if (tempnext > totalPages)
        {
            tempnext = totalPages;
        }
        pagesiguiente.Attributes.Add("onclick", "document.location.href='TipsJardin.aspx?c=6&pn=" + tempnext.ToString() + (!string.IsNullOrEmpty(c) ? "&cat=" + c : "") + "';");
        paginator.Controls.Add(pagesiguiente);

        var br = new HtmlGenericControl("br");
        paginator.Controls.Add(br);

        //End of paginator
    }

    protected HtmlGenericControl GetSocialPlugInDiv(string title, string summary, string url)
    {
        var c = new HtmlGenericControl("div");
        //c.ID = Guid.NewGuid().ToString().Replace("-", "");
        c.Attributes.Add("class", "addthis_toolbox addthis_default_style addthis_32x32_style");
        c.Attributes.Add("addthis:url", url);
        c.Attributes.Add("addthis:title", title);
        c.Attributes.Add("addthis:description", summary);
        c.Style.Add(HtmlTextWriterStyle.TextAlign, "right");

        //Facebook
        var s = new HtmlGenericControl("a");
        s.Attributes.Add("class", "addthis_button_preferred_1");
        s.Attributes.Add("addthis:url", url);
        s.Attributes.Add("addthis:title", title);
        s.Attributes.Add("addthis:description", summary);
        c.Controls.Add(s);
        //twitter
        var s1 = new HtmlGenericControl("a");
        s1.Attributes.Add("class", "addthis_button_preferred_2");
        c.Controls.Add(s1);
        //googleplus
        var s2 = new HtmlGenericControl("a");
        s2.Attributes.Add("class", "addthis_button_preferred_3");
        c.Controls.Add(s2);
        //email
        var s3 = new HtmlGenericControl("a");
        s3.Attributes.Add("class", "addthis_button_google_plusone");
        c.Controls.Add(s3);
        return c;
    }
    protected void InitPage()
    {
        sectionTop.ImageUrl = "~/Images/tipsjardin/tipsjardin.jpg";
        LoadBanners();
    }

    protected void LoadBanners()
    {
        var banner1 = new ServiceUI().GetBanner(BannerType.Top);
        for (int i = 0; i < banner1.Count(); i++)
        {
            var c = new HyperLink() { ImageUrl = banner1[i].Url, NavigateUrl = banner1[i].LinkUrl };
            var p = new HyperLink() { NavigateUrl = banner1[i].LinkUrl, Text = ((i + 1)).ToString() };
            c.Target = "_blank";
            p.Attributes.Add("rel", ((i + 1)).ToString());
            imagereel.Controls.Add(c);
            paging.Controls.Add(p);
        }
        var banner2 = new ServiceUI().GetBanner(BannerType.Middle);
        for (int i = 0; i < banner2.Count(); i++)
        {
            var c = new HyperLink() { ImageUrl = banner2[i].Url, NavigateUrl = banner2[i].LinkUrl };
            var p = new HyperLink() { NavigateUrl = banner2[i].LinkUrl, Text = ((i + 1)).ToString() };
            c.Target = "_blank";
            p.Attributes.Add("rel", ((i + 1)).ToString());
            imagereel2.Controls.Add(c);
            paging2.Controls.Add(p);
        }
        var banner3 = new ServiceUI().GetBanner(BannerType.Bottom);
        for (int i = 0; i < banner3.Count(); i++)
        {
            var c = new HyperLink() { ImageUrl = banner3[i].Url, NavigateUrl = banner3[i].LinkUrl };
            var p = new HyperLink() { NavigateUrl = banner3[i].LinkUrl, Text = ((i + 1)).ToString() };
            c.Target = "_blank";
            p.Attributes.Add("rel", ((i + 1)).ToString());
            imagereel3.Controls.Add(c);
            paging3.Controls.Add(p);
        }
    }


}